package com.javarush.test.level26.lesson15.big01;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by admin on 08.04.2015.
 */
public class CurrencyManipulator {
    private String currencyCode;
    private Map<Integer, Integer> denominations=new HashMap<>(); // номинал - колличество

    public CurrencyManipulator(String currencyCode) {
        this.currencyCode = currencyCode;
    }

    public String getCurrencyCode() {
        return currencyCode;
    }

    public boolean hasMoney(){
        if(getTotalAmount()==0) return false;
        return true;
    }

    public boolean isAmountAvailable(int expectedAmount){
        return getTotalAmount()>=expectedAmount;
    }

    public Map<Integer, Integer> withdrawAmount(int expectedAmount){
        Map<Integer,Integer> map=new HashMap<>();

        Map<Integer,Integer> m=new TreeMap<Integer,Integer>(new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return (o1 > o2) ? -1 : ((o1 == o2) ? 0 : 1);}
            });

        Map<Integer,Integer> ways=new HashMap<>();
        m.putAll(denominations);
        int counter=0;
        int sum=0;

        for(Map.Entry<Integer,Integer> entry:m.entrySet()){
            
            Integer nominal=entry.getKey();
            ways.put(nominal,+1);
            int value=entry.getValue();
            for(int i=0; i<value;i++){
                if((sum+nominal)>expectedAmount) continue;
                sum=sum+nominal;
            }



            counter++;

        }


        return map;
    }


    public void addAmount(int denomination, int count){
        if (denominations.containsKey(denomination))
        {
            denominations.put(denomination, denominations.get(denomination) + count);
        } else
        {
            denominations.put(denomination, count);
        }
    }

    public int getTotalAmount(){
        int result=0;
        for(Map.Entry<Integer,Integer> pair :denominations.entrySet()){

            result = result+(pair.getKey()*pair.getValue());
        }
        return result;
    }
}
